package queue;

import java.util.LinkedList;

/**
 * @description: 150. 逆波兰表达式求值
 * https://leetcode.cn/problems/evaluate-reverse-polish-notation/
 * @author: caoyihong
 * @date: 2025-10-11 15:18
 * @Version 1.0
 **/
public class EvalRPN {
    public int evalRPN(String[] tokens) {
        LinkedList<String> list = new LinkedList<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                String s1 = list.removeFirst();
                String s2 = list.removeFirst();
                int result = Integer.parseInt(s2) + Integer.parseInt(s1);
                list.addFirst(result+"");
            } else if (token.equals("-")) {
                String s1 = list.removeFirst();
                String s2 = list.removeFirst();
                int result = Integer.parseInt(s2) - Integer.parseInt(s1);
                list.addFirst(result+"");
            } else if (token.equals("*")) {
                String s1 = list.removeFirst();
                String s2 = list.removeFirst();
                int result = Integer.parseInt(s2) * Integer.parseInt(s1);
                list.addFirst(result+"");
            } else if (token.equals("/")) {
                String s1 = list.removeFirst();
                String s2 = list.removeFirst();
                int result = Integer.parseInt(s2) / Integer.parseInt(s1);
                list.addFirst(result+"");
            } else {
                list.addFirst(token);
            }
        }
        return Integer.parseInt(list.pop());
    }
}
